Function GetRandomMath(m,n)
  Randomize
  GetRandomMath = Int(((n-m+1) * Rnd) + m)
End Function
'###################################################################
'###################################################################
'###################################################################
'###################################################################
''' ExpeditionPCB Application
Private Function ValidateServer(docObj)
    Dim key, licenseServer, licenseToken
    key = docObj.Validate(0)
    Set licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")
    licenseToken = licenseServer.GetToken(key)
    Set licenseServer = nothing
    'On Error Resume Next
    Err.Clear
    docObj.Validate(licenseToken)
    If Err Then
        ValidateServer = 0    
    Else 
        ValidateServer = 1
    End If
End Function
' 执行脚本的公共方法
sub executeScript(filepath)
    dim ocmd:ocmd = "xs " & filepath
	'msgbox ocmd
    application.gui.processkeyin(ocmd)
	'msgbox "run"
end sub

'###################################################################
'###################################################################
'###################################################################
'###################################################################
''' File System Object
function IsFolderPath(oPath)
	Set fs = CreateObject("Scripting.FileSystemObject")
	' 检查路径是否存在
	If Not fs.FolderExists(oPath) And Not fs.FileExists(oPath) Then
		IsFolderPath=false
	else
		' 判断是否为目录
		If fs.FolderExists(oPath) Then
			IsFolderPath=true
		Else
			IsFolderPath=false
		End If
	End If
end function
sub WriteOstrToFile(ofile,ostr)
	dim fso,fw
	set fso = createObject("scripting.filesystemobject")
	set fw = fso.createTextfile(ofile,1)
	call fw.write(ostr)
	'msgbox "1"
	fw.close()
	'msgbox "2"
	set fw = nothing
	set fso = nothing			
end sub
' 核心读取函数（支持UTF-8/ANSI＿
'encodingType = "UTF-8"  ' 默认编码
'fileContent = ReadTextFile(filePath, encodingType)	
Function ReadTextFile(filePath, charset)
    Dim stream, content
    Set stream = CreateObject("ADODB.Stream")
    
    With stream
        .Type = 2              ' 文本模式
        .Mode = 3              ' 读写权限
        .Charset = charset     ' 编码类型（推荿UTF-8"房GB2312"＿
        .Open
        .LoadFromFile filePath ' 加载文件
        content = .ReadText    ' 读取内容
        .Close
    End With
    
    Set stream = Nothing
    ReadTextFile = content
End Function

'###################################################################
'###################################################################
'###################################################################
'###################################################################
''' String Options
function removeMultiSpace(tmp)
	Set regEx = New RegExp
	regex.Global = True ' 全局匹配
	regex.IgnoreCase = True ' 不区分大小写
	regex.Pattern = " +" ' 匹配一个或多个空格
	tmp = regEx.Replace(tmp, " ")
	Set regEx = Nothing
	
	removeMultiSpace = tmp
end function
' 判断字符串是否包含目标内宿
Function ContainsString(content, target)
    ContainsString = InStr(1, content, target, vbTextCompare) > 0
End Function
'###################################################################
'###################################################################
'###################################################################
'###################################################################
''' user layer
sub CheckUserlyr(ulyr)
	Set usrLyrObj = doc.FindUserLayer(ulyr)
	If usrLyrObj Is Nothing Then
		Set usrLyrObj = doc.SetupParameter.PutUserLayer(ulyr)
	Else
		doc.UserLayerGfxs(, ulyr).Delete
	End If
	' TOP level
	Dim displCtrlObj
	Set displCtrlObj = doc.ActiveView.DisplayControl
	Dim dispCtrlGlobalObj
	Set dispCtrlGlobalObj = displCtrlObj.Global
	dispCtrlGlobalObj.UserLayerDisplayLevel(ulyr) = epcbDisplayLevelTop
	dispCtrlGlobalObj.RaiseUserLayerDisplayLevel(ulyr)
	displCtrlObj.UserLayer(ulyr)=True
end sub
sub HighlightUserlyr(ulyr)
	Set usrLyrObj = doc.FindUserLayer(ulyr)
	If usrLyrObj Is Nothing Then
		Set usrLyrObj = doc.SetupParameter.PutUserLayer(ulyr)
	Else
		'doc.UserLayerGfxs(, ulyr).Delete
	End If
	' TOP level
	Dim displCtrlObj
	Set displCtrlObj = doc.ActiveView.DisplayControl
	Dim dispCtrlGlobalObj
	Set dispCtrlGlobalObj = displCtrlObj.Global
	if displCtrlObj.UserLayer(ulyr)=false then
		dispCtrlGlobalObj.UserLayerDisplayLevel(ulyr) = epcbDisplayLevelTop
		dispCtrlGlobalObj.RaiseUserLayerDisplayLevel(ulyr)
		displCtrlObj.UserLayer(ulyr)=True
	else
		displCtrlObj.UserLayer(ulyr)=false
	end if
end sub
sub CheckUserlyr_withColor(ulyr,colobj)
	dim usrLyrObj
	Set usrLyrObj = doc.FindUserLayer(ulyr)
	If usrLyrObj Is Nothing Then
		Set usrLyrObj = doc.SetupParameter.PutUserLayer(ulyr)
	Else
		doc.UserLayerGfxs(, ulyr).Delete
	End If
	' TOP level
	Dim displCtrlObj
	Set displCtrlObj = doc.ActiveView.DisplayControl
	Dim dispCtrlGlobalObj
	Set dispCtrlGlobalObj = displCtrlObj.Global
	dispCtrlGlobalObj.UserLayerDisplayLevel(ulyr) =0
	dispCtrlGlobalObj.RaiseUserLayerDisplayLevel(ulyr)
	displCtrlObj.UserLayer(ulyr)=True
	'red color	
	set doc.activeView.DisplayControl.Global.UserLayerColor(ulyr)=colobj
	
end sub
Function AddUsrMarks(userLayerName,oMarks,ObjX,ObjY)
	Set usrLyrObj = doc.FindUserLayer(userLayerName)
    Dim ch
    Dim pen
    Dim textOrientation
    pen = 0
	ch = 0.254		
	Call doc.PutUserLayerText(oMarks,ObjX,ObjY, usrLyrObj,ch,textOrientation,pen,,1,,,nothing)
End Function

Function AddUsrGfx(userLayerName,pntArr)
	dim numPoints:numPoints = UBound(pntArr, 2) + 1
	Set usrLyrObj = doc.FindUserLayer(userLayerName)
	set fabGfxObj = doc.PutUserLayerGfx(usrLyrObj,1.0,numPoints,pntArr,true,Nothing,epcbUnitCurrent)	
End Function
sub TopUserLyr(ulyr)
	Dim usrLyrObj
	Set usrLyrObj = doc.FindUserLayer(ulyr)
	If usrLyrObj Is Nothing Then exit sub
	
	' TOP level
	Dim displCtrlObj
	Set displCtrlObj = doc.ActiveView.DisplayControl
	Dim dispCtrlGlobalObj
	Set dispCtrlGlobalObj = displCtrlObj.Global
	dispCtrlGlobalObj.UserLayerDisplayLevel(ulyr) = epcbDisplayLevelTop
	dispCtrlGlobalObj.RaiseUserLayerDisplayLevel(ulyr)
	displCtrlObj.UserLayer(ulyr)=True
	Set usrLyrObj = nothing
end sub
sub ClearUserlyr(ulyr)
	Dim usrLyrObj
	Set usrLyrObj = doc.FindUserLayer(ulyr)
	If usrLyrObj Is Nothing Then exit sub
	doc.UserLayerGfxs(, ulyr).Delete
	doc.UserLayerTexts(, ulyr).Delete
	
	Set usrLyrObj = nothing
end sub
sub AddUserlyr(ulyr)
	Dim usrLyrObj
	Set usrLyrObj = doc.FindUserLayer(ulyr)
	If not usrLyrObj Is Nothing Then exit sub
	Set usrLyrObj = doc.SetupParameter.PutUserLayer(ulyr)
	set usrLyrObj = nothing
end sub
